package com.bjpowernode.crm.workbench.service.impl;

import com.bjpowernode.crm.utils.DateTimeUtil;
import com.bjpowernode.crm.utils.SqlSessionUtil;
import com.bjpowernode.crm.utils.UUIDUtil;
import com.bjpowernode.crm.vo.PaginationVO;
import com.bjpowernode.crm.workbench.dao.CustomerDao;
import com.bjpowernode.crm.workbench.dao.TranDao;
import com.bjpowernode.crm.workbench.dao.TranHistoryDao;
import com.bjpowernode.crm.workbench.domain.Activity;
import com.bjpowernode.crm.workbench.domain.Customer;
import com.bjpowernode.crm.workbench.domain.Tran;
import com.bjpowernode.crm.workbench.domain.TranHistory;
import com.bjpowernode.crm.workbench.service.TranService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: crm
 * @description:
 * @author: 玉树临风的寒
 * @create: 2020-12-24 17:07:12
 **/
public class TranServiceImpl implements TranService {

    private TranDao tranDao = SqlSessionUtil.getSqlSession().getMapper(TranDao.class);
    private TranHistoryDao tranHistoryDao = SqlSessionUtil.getSqlSession().getMapper(TranHistoryDao.class);
    private CustomerDao customerDao = SqlSessionUtil.getSqlSession().getMapper(CustomerDao.class);


    public boolean save(Tran t, String customerName) {
        /*
        交易添加业务：
          在做添加之前 ，参数t里面就少了一项信息 就是客户的customerId
          先处理客户相关的需求
          1.判断customerName，根据客户名称在客户表进行精确查询
          如果 有这个客户则取出这个客户的id 封装到t对象中
          如果没有这个客户，则在客户表新建一条客户信息，然后将新建的客户的id取出
          封装到t对象中
          2. 经过以上操作后 t对象的信息完善，需要执行添加交易的操作
          3.添加操作完毕后，需要创建一条交易历史
         */
        boolean flag = true;

        //1.
        Customer cus = customerDao.getCustomerByName(customerName);
        if(cus==null){
            //如果为空创建一个客户
            cus = new Customer();
            cus.setId(UUIDUtil.getUUID());
            cus.setName(customerName);
            cus.setCreateBy(t.getCreateBy());
            cus.setCreateTime(DateTimeUtil.getSysTime());
            cus.setContactSummary(t.getContactSummary());
            cus.setNextContactTime(t.getNextContactTime());
            cus.setOwner(t.getOwner());
            //添加客户
            int count1=customerDao.save(cus);
            if (count1!=1){
                flag=false;
            }

        }

        //通过以上对于客户的处理，无论是查询出已有的客户，还是以前没有现在新建的客户
        //但是现在客户已经有了 id也有了
        //将客户的id封装到t对象中
        t.setCustomerId(cus.getId());

        //添加交易
        int count2 = tranDao.save(t);
        if(count2!=1){
            flag=false;
        }
        //添加交易历史
        TranHistory th = new TranHistory();
        th.setId(UUIDUtil.getUUID());
        th.setTranId(t.getId());
        th.setStage(t.getStage());
        th.setMoney(t.getMoney());
        th.setExpectedDate(t.getExpectedDate());
        th.setCreateTime(DateTimeUtil.getSysTime());
        th.setCreateBy(t.getCreateBy());

        int count3 = tranHistoryDao.save(th);
        if(count3!=1){
            flag=false;
        }


        return flag;
    }

    public Tran detail(String id) {

        Tran t=tranDao.detail(id);
        return t;
    }

    public PaginationVO<Tran> pageList(Map<String, Object> map) {
        int total = tranDao.getTotalByCondition(map);

        List<Tran> dataList = tranDao.getActivityListByCondition(map);


        PaginationVO<Tran> vo = new PaginationVO<Tran>();
        vo.setTotal(total);
        vo.setDataList(dataList);
        return vo;
    }

    public List<TranHistory> getHistoryListByTranId(String tranId) {

        List<TranHistory> thList= tranHistoryDao.getHistoryListByTranId(tranId);

        return thList;
    }

    public boolean changeStage(Tran t) {
        boolean flag = true;

        //改变交易阶段
        int count1=tranDao.changeStage(t);
        if(count1!=1){
            flag=false;
        }

        //交易阶段改变后生成一条交易历史
        TranHistory th =new TranHistory();
        th.setId(UUIDUtil.getUUID());
        th.setStage(t.getStage());
        th.setPossibility(t.getPossibility());
        th.setCreateBy(t.getEditBy());
        th.setCreateTime(DateTimeUtil.getSysTime());
        th.setExpectedDate(t.getExpectedDate());
        th.setMoney(t.getMoney());
        th.setTranId(t.getId());
        //添加交易历史
        int count2=tranHistoryDao.save(th);
        if(count2!=1){
            flag=false;
        }
        return flag;
    }

    public Map<String, Object> getCharts() {

        //取得total
        int total=tranDao.getTotal();

        //取得dataList
        List<Map<String,Object>> dataList=tranDao.getCharts();

        //取得所有名字
        List<String> names = tranDao.getNames();

        //将total和dataList保存到map中
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("dataList",dataList);
        map.put("total",total);
        map.put("names",names);
        //返回map
        return map;

    }
}
